﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace Sharper
{
    /// <summary>
    /// 应用程序相关的基础数据,#ToDo放到Revit开发库中!
    /// </summary>
    public static class TheAppBase
    {
        #region Dll路径及Dll的父路径

        private static string _dllDir = null; //dll文件所在路径
        private static string _dllParent = null; //dll文件所在路径的父路径
        private static string _rootDir = null; //bin文件夹


        /// <summary>
        ///当前"应用"dll所在目录,结尾不包括"\\"
        /// </summary>
        /// <returns></returns>
        public static string AppDllDir
        {
            get
            {
                if (!String.IsNullOrEmpty(_dllDir))
                {
                    return _dllDir;
                }

                string location = Assembly.GetExecutingAssembly().Location; //dll全路径
                _dllDir = System.IO.Path.GetDirectoryName(location); //返回父亲路径,结尾不包括"\\"

                return _dllDir;
            }
        }

        /// <summary>
        ///当前"应用"dll所在目录的父目录,结尾不包括"\\"
        /// </summary>
        /// <returns></returns>
        public static string AppParentDir
        {
            get
            {
                if (!String.IsNullOrEmpty(_dllParent))
                {
                    return _dllParent;
                }

                try
                {
                    DirectoryInfo dr1 = new DirectoryInfo(AppDllDir);
                    Debug.Assert(dr1 != null);
                    DirectoryInfo dr2 = dr1.Parent;
                    Debug.Assert(dr2 != null, nameof(dr2) + " != null");
                    _dllParent = dr2.FullName; //返回父亲路径

                    return _dllParent;
                }
                catch (Exception e)
                {
                    MessageBox.Show("获得dll父路径失败!" + e.Message);
                    return "Error";
                }
            }
        }


        /// <summary>
        /// 约定的安装根路径,目前在dll的上层的,上层
        /// </summary>
        public static string RootDir
        {
            get
            {
                if (!String.IsNullOrEmpty(_rootDir))
                {
                    return _rootDir;
                }

                try
                {
                    DirectoryInfo dr1 = new DirectoryInfo(AppParentDir);
                    Debug.Assert(dr1 != null);
                    DirectoryInfo dr2 = dr1.Parent;
                    Debug.Assert(dr2 != null, nameof(dr2) + " != null");
                    _rootDir = dr2.FullName; //返回父亲路径

                    return _rootDir;
                }
                catch (Exception e)
                {
                    MessageBox.Show("获得Bin父路径失败!" + e.Message);
                    return "Error";
                }
            }
        }

        #endregion

        #region 数据文件夹Data路径

        private static readonly string m_dataPath = String.Format("{0}\\{1}", RootDir, "Data");

        /// <summary>
        /// 数据文件夹,不包括尾部的"\" 放到Data目录下了.
        /// </summary>
        public static string StdDataDir
        {
            get
            {
                if (!Directory.Exists(m_dataPath))
                {
                    Directory.CreateDirectory(m_dataPath);
                }

                return m_dataPath;
            }
        }

        #endregion


        #region 临时文件路径

        /// <summary>
        /// 临时文件存放路径,不包括尾部的"\"  
        /// </summary>
        public static string TmpFileDir
        {
            get
            {
                if (!Directory.Exists(m_tmpRfaFilePath))
                {
                    Directory.CreateDirectory(m_tmpRfaFilePath);
                }

                return m_tmpRfaFilePath;
            }
        }

        private static readonly string m_tmpRfaFilePath = AppParentDir + "\\Temp";

        #endregion


        #region Log日志的输出路径

        /// <summary>
        ///  Log日志的输出路径
        /// </summary>
        public static string LogFolder
        {
            get
            {
                //string folder = Path.Combine(AppParentDir, LoggerFolderName);
                string folder = Path.Combine(RootDir, LoggerFolderName);

                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }


                return folder;
            }
        }

        private const string LoggerFolderName = "Logger";

        #endregion


        #region 默认配置文件

        /// <summary>
        /// 默认ini文件(UcdTunnel.ini)的路径
        /// </summary>
        public static string DefaultIniPath
        {
            get
            {
                string path = System.IO.Path.Combine(AppDllDir, "RevitSharperStudio.ini");
                if (!File.Exists(path))
                {
                    File.Create(path);
                    //TaskDialog.Show("提示(仅首次启动程序弹出此对话框)", "默认ini文件不存在,已自动创建.! \n路径:" + path);
                }

                return path;
            }
        }

        #endregion
    }
}